/**
 * 工单相关工具函数
 */
// @ts-ignore
import { Values } from '@/enums';

/**
 * 从枚举对象创建映射表
 */
export const createMapFromEnum = (enumObj: any): Record<number, { text: string; color: string }> => {
  const map: Record<number, { text: string; color: string }> = {};
  Object.keys(enumObj).forEach(key => {
    const numKey = parseInt(key);
    map[numKey] = {
      text: enumObj[key].desc,
      color: enumObj[key].color
    };
  });
  return map;
};

/**
 * 工单类型映射
 */
export const getWorkOrderTypeMap = () => createMapFromEnum(Values.WorkOrderType);

/**
 * 工单优先级映射
 */
export const getWorkOrderPriorityMap = () => createMapFromEnum(Values.WorkOrderPriority);

/**
 * 工单状态映射
 */
export const getWorkOrderStatusMap = () => createMapFromEnum(Values.WorkOrderStatus);

/**
 * 根据工作项目获取显示文本
 */
export const getWorkOrderTypeText = (type: number): string => {
  return getWorkOrderTypeMap()[type]?.text || '未知类型';
};

/**
 * 根据工单优先级获取显示文本
 */
export const getWorkOrderPriorityText = (priority: number): string => {
  return getWorkOrderPriorityMap()[priority]?.text || '未知优先级';
};

/**
 * 根据工单状态获取显示文本
 */
export const getWorkOrderStatusText = (status: number): string => {
  return getWorkOrderStatusMap()[status]?.text || '未知状态';
};

/**
 * 根据工单类型获取颜色
 */
export const getWorkOrderTypeColor = (type: number): string => {
  return getWorkOrderTypeMap()[type]?.color || 'default';
};

/**
 * 根据工单优先级获取颜色
 */
export const getWorkOrderPriorityColor = (priority: number): string => {
  return getWorkOrderPriorityMap()[priority]?.color || 'default';
};

/**
 * 根据工单状态获取颜色
 */
export const getWorkOrderStatusColor = (status: number): string => {
  return getWorkOrderStatusMap()[status]?.color || 'default';
};

/**
 * 判断工单是否可以派单
 */
export const canDispatchWorkOrder = (status: number): boolean => {
  return status === 0; // 只有待指派状态可以派单
};

/**
 * 判断工单是否可以编辑
 */
export const canEditWorkOrder = (status: number): boolean => {
  return status !== 7; // 除了已关闭状态，其他状态都可以编辑
};

/**
 * 判断工单是否可以删除
 */
export const canDeleteWorkOrder = (status: number): boolean => {
  return status === 0 || status === 6; // 只有待指派和已取消状态可以删除
}; 